// pages/myself/integral/Integral.js
var Zan = require('../../../dist/index.js');
let WxNotificationCenter = require('../../../utils/WxNotificationCenter.js');
let network = null;
let utils = null;
let api = null;
const app = getApp();
var start = 0;
Page(Object.assign({}, Zan.TopTips, Zan.Toast,{

  /**
   * 页面的初始数据
   */
  data: {
    userinfo: {},
    integiftList: [],
    integral: 0,
    storeId: null,
    recordsTotal:0,
    nodata: {
      msg: '暂无内容',
      notype: 2,
      imgurl: '../../../images/nodatas.png',
      nourl: ''
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    network = new app.NetWorkUtils();
    utils = new app.Utils();
    api = new app.Api();
    if (app.globalData.userInfo) {
      this.setData({
        integral: app.globalData.userInfo.integral
      })
    } else {
      // 由于 openId 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoCallback = res => {
        this.setData({
          integral: res.integral
        })
      }
    }
    // 注册列表刷新事件
    WxNotificationCenter.addNotification("integralRefreshNotification", this.refreshNotification, this);
    this.onPullDownRefresh();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    WxNotificationCenter.removeNotification("integralRefreshNotification", this);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    start = 0;
    this.setData({
      integiftList:[]
    })
    network._post_json(this, api.getUserInfo, { appId: app.globalData.appId, openId: app.globalData.openId }, getUserInfosuccess);
    network._post_page_json(this, api.integiftList, start, { storeId: app.globalData.store.id}, success);

  },
  refreshNotification: function () {
    this.onPullDownRefresh();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.integiftList.length < this.data.recordsTotal) {
      start = start + 20;
      network._post_page_json(this, api.integiftList, start, { storeId: app.globalData.store.id }, success);
    }
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {
  
  // },
  // integralRule:function(){
  //   wx.navigateTo({
  //     url: '/pages/myself/integral/IntegralRules',
  //   })
  // },
  userDetails: function () {
    wx.navigateTo({
      url: '/pages/myself/integral/IntegralDetails'
    })
  },
  ongroupItemClick:function(e){
    var productId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/myself/integral/RedeemDetails?productId=' + productId + '&storeId=' + app.globalData.store.id,
    })
  }
}))

var success = function(that,res){
  if(res.data.data){
    that.setData({
      integiftList: res.data.data,
      recordsTotal: res.data.data.length
    })
  }
}
var getUserInfosuccess = function(that,res){
  if(res.data.data){
    app.globalData.userInfo = res.data.data;
  }
}